/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "memoryinformation.h"
#include <sys/statfs.h>

MemoryInformation::MemoryInformation()
{
}

//获取CPU的利用率
double MemoryInformation::cal_cpuoccupy (CPU_OCCUPY *o, CPU_OCCUPY *n)
{
    double od, nd;
    double id, sd;
    double cpu_use ;

    od = (double) (o->user + o->nice + o->system +o->idle+o->softirq+o->iowait+o->irq);//第一次(用户+优先级+系统+空闲)的时间再赋给od
    nd = (double) (n->user + n->nice + n->system +n->idle+n->softirq+n->iowait+n->irq);//第二次(用户+优先级+系统+空闲)的时间再赋给od

    id = (double) (n->idle);    //用户第一次和第二次的时间之差再赋给id
    sd = (double) (o->idle) ;    //系统第一次和第二次的时间之差再赋给sd
    if((nd-od) != 0)
    cpu_use =1.0- ((id-sd))/(nd-od)*1.00; //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
    else cpu_use = 0;
    return cpu_use;
}

void MemoryInformation::get_cpuoccupy (CPU_OCCUPY *cpust)
{
    FILE *fd;
    int n;
    char buff[256];
    CPU_OCCUPY *cpu_occupy;
    cpu_occupy=cpust;

    fd = fopen ("/proc/stat", "r");
    fgets (buff, sizeof(buff), fd);//core dump

    sscanf (buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,&cpu_occupy->system, &cpu_occupy->idle ,&cpu_occupy->iowait,&cpu_occupy->irq,&cpu_occupy->softirq);

    fclose(fd);
}

double MemoryInformation::getCpuRate()
{
    CPU_OCCUPY cpu_stat1;
    CPU_OCCUPY cpu_stat2;
    double cpu;
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);
    sleep(1);

    //第二次获取cpu使用情况
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);

    //计算cpu使用率
    cpu = cal_cpuoccupy ((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2);

    return cpu;
}

//获取内存的使用情况
MEM_PACK MemoryInformation::get_memoccupy()    // get RAM message
{
    FILE *fd;
    int n;
    double mem_total,mem_used_rate;;
    char buff[256];
    MEM_OCCUPY *m=(MEM_OCCUPY *)malloc(sizeof(MEM_OCCUPY));;
    MEM_PACK *p=(MEM_PACK *)malloc(sizeof(MEM_PACK));
    fd = fopen ("/proc/meminfo", "r");

    fgets (buff, sizeof(buff), fd);
    sscanf (buff, "%s %lu %s\n", m->name, &m->total, m->name2);
    mem_total=m->total;
    fgets (buff, sizeof(buff), fd);//core dump
    sscanf (buff, "%s %lu %s\n", m->name, &m->total, m->name2);
    mem_used_rate=(1-m->total/mem_total);
    mem_total=mem_total/(1024);
    p->total=mem_total;
    p->used_rate=mem_used_rate;
    fclose(fd);     //关闭文件fd

    MEM_PACK tmpReturn=*p;
    free(p);


    return tmpReturn ;
}

DEV_MEM MemoryInformation::get_devmem2()
{
    //--------磁盘信息测试---------------------------
    struct statfs diskInfo;
    statfs("/", &diskInfo);

    double totalDiskSize = (double)(diskInfo.f_bsize*diskInfo.f_blocks);
    double usedDiskSize = (double)(diskInfo.f_bsize*diskInfo.f_blocks-diskInfo.f_bsize*diskInfo.f_bfree);

    DEV_MEM  *dev=(DEV_MEM *)malloc(sizeof(DEV_MEM));
    dev->total=totalDiskSize/1024.0/1024.0;;
    dev->used_rate=usedDiskSize/totalDiskSize;

    DEV_MEM tmpReturn=*dev;
    free(dev);
    return tmpReturn ;

}

//获取硬盘的使用情况
DEV_MEM MemoryInformation::get_devmem()        // get hard disk meeeage
{
        FILE * fp;
        int h=0;
        char buffer[80],a[80],d[80],e[80],f[80],buf[256];
        double c,b;
        fp=popen("df","r");
        fgets(buf,256,fp);
        double dev_total=0,dev_used=0;
        DEV_MEM  *dev=(DEV_MEM *)malloc(sizeof(DEV_MEM));
        while(6==fscanf(fp,"%s %lf %lf %s %s %s",a,&b,&c,d,e,f))
        {
                dev_total+=b;
                dev_used+=c;
        }
        dev->total=dev_total/1024;;
        dev->used_rate=dev_used/dev_total;
        pclose(fp);

        DEV_MEM tmpReturn=*dev;
        free(dev);
        return tmpReturn ;
}
